package com.CityHunter.Request;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.util.Log;

import com.CityHunter.Common.Http;
import com.CityHunter.DataObject.User;

public class LoginRequest {
	
	private String loginPage;
	
	public LoginRequest(String loginPage){
		this.loginPage = loginPage;
	}
	
	public User login(String email,String password){
		
		User user = new User();
		
		// Declare NameValuePair to store the data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user", email));
		params.add(new BasicNameValuePair("pass", password));

		// Send loginRequest
		Http http = new Http();
		Log.d("Sttp_Debug", "Sending Request : " + loginPage);
		JSONArray jsonResponse = http.sendRequest(loginPage, params);
		try {
			user.setUserid(Integer.parseInt(jsonResponse.getJSONObject(0)
					.getString("userid")));
			user.setName(jsonResponse.getJSONObject(0).getString("name"));
		} catch (Exception e) {
			Log.e("Sttp_Error", e.getMessage().toString());
		}
		
		return user;
	}
}
